package manager
{
	import data.Globals;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundLoaderContext;
	import flash.net.URLRequest;
	
	import ui.ria.ClickSound;
	import ui.ria.ConnectSound;
	
	/**
	 *
	 *@author CrazyChen 
	 *2013-4-7
	 */	
	public class SoundManage
	{
		/*** 手榴弹 */		
		public static var HAND_GRENADE:String = "handexplode";
		/*** 子弹击中 */		
		public static var BULLET_ART:String = "bulletArt";
		/*** 安装武器 */		
		public static var SITE_ARMS:String = "sitDown";
		
		private const MP3URL:String = "mp3/";
		
		private static var _instance:SoundManage;
		
		private var soundMap:Object;
		
		private var lightning:ConnectSound;
		
		private  var btnClick:ClickSound;
		
		private var sndChannel:SoundChannel;

		public function SoundManage()
		{
			if(_instance)throw new Error("this is a singerleton");
			soundMap={};
		}
		
		
		public static function getInstance():SoundManage{
			return _instance ||=new SoundManage();
		}
		
		public function playOrStopLightning(isPlay:Boolean):void{
			if(!lightning)lightning = new ConnectSound();
			if(isPlay){
				sndChannel = lightning.play(0);
			}else{
				sndChannel.stop();
			}
		}
		
		public function playBtnClick():void{
			if(!btnClick)btnClick = new ClickSound();
			btnClick.play(0,1);
		}

		public function play(name:String):void{
			var url:String = Globals.RES_URL + MP3URL + name+".mp3";
			var snd:Sound;
			if(soundMap[url]){
				snd= soundMap[url];
			}else{
				snd= new Sound(new URLRequest(url),new SoundLoaderContext(2000));
				snd.addEventListener(IOErrorEvent.IO_ERROR,ioError);
				snd.addEventListener(Event.COMPLETE,complete);
			}
			snd.play();
		}
		
		protected function ioError(event:IOErrorEvent):void
		{
			trace("地址错误");
		}
		
		private function complete(e:Event):void
		{
			var snd:Sound = e.target as Sound;
			soundMap[snd.url] = snd;
		}
	}
}
